/**
 * create time : 2010-5-21
 * author : Administrator
 */
package com.xbang.core.page;

import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Administrator
 * 
 */
public class PageUtils {

	private static final int DEFAULT_CURRENT_PAGE = 1;

	private static final Log logger = LogFactory.getLog(PageUtils.class);

	/**
	 * Use the origin page to create a new page
	 * 
	 * @param page
	 * @param totalRecords
	 * @return
	 */
	public static Page createPage(Page page, int totalRecords) {
		String selectCheckIds = page.getSelectCheckIds();
		String actionURI = page.getActionURI();
		page = createPage(page.getEveryPageSize(), page.getCurrentPage(), totalRecords);
		page.setSelectCheckIds(selectCheckIds);
		page.setActionURI(actionURI);
		return page;
	}

	/**
	 * the basic page utils not including exception handler
	 * 
	 * @param everyPageSize
	 * @param currentPage
	 * @param totalRecords
	 * @return page
	 */
	public static Page createPage(int everyPageSize, int currentPage, int totalRecords) {
		everyPageSize = getEveryPageSize(everyPageSize);
		currentPage = getCurrentPage(currentPage);
		int beginIndex = getBeginIndex(everyPageSize, currentPage);
		int totalPage = getTotalPage(everyPageSize, totalRecords);
		boolean prePage = hasPrePage(currentPage);
		boolean nextPage = hasNextPage(currentPage, totalPage);
		return new Page(prePage, nextPage,currentPage,  everyPageSize, totalPage, beginIndex);
	}

	/**
	 * 得到开始列
	 * 
	 * @param everyPage
	 * @param currentPage
	 * @return
	 */
	public static int getBeginIndex(int everyPageSize, int currentPage) {
		return (currentPage - 1) * everyPageSize;
	}

	/**
	 * To handle the page size
	 * 
	 * @param everyPageSize
	 * @return
	 */
	private static int getEveryPageSize(int everyPageSize) {
		String defaultEveryPage = ResourceBundle.getBundle("config").getString("page.everyPage");
		return everyPageSize == 0 ? new Integer(defaultEveryPage).intValue() : everyPageSize;
	}

	/**
	 * To handle the current page
	 * 
	 * @param currentPage
	 * @return
	 */
	private static int getCurrentPage(int currentPage) {
		return currentPage == 0 ? DEFAULT_CURRENT_PAGE : currentPage;
	}


	/**
	 * To handle total page
	 * 
	 * @param everyPage
	 * @param totalRecords
	 * @return
	 */
	private static int getTotalPage(int everyPage, int totalRecords) {
		int totalPage = 0;

		if (totalRecords % everyPage == 0)
			totalPage = totalRecords / everyPage;
		else
			totalPage = totalRecords / everyPage + 1;

		return totalPage;
	}

	/**
	 * To check if the page instance has pre page
	 * 
	 * @param currentPage
	 * @return
	 */
	private static boolean hasPrePage(int currentPage) {
		return currentPage == DEFAULT_CURRENT_PAGE ? false : true;
	}

	/**
	 * To check if the page instance has next page
	 * 
	 * @param currentPage
	 * @param totalPage
	 * @return
	 */
	private static boolean hasNextPage(int currentPage, int totalPage) {
		return currentPage == totalPage || totalPage == 0 ? false : true;
	}
}
